

<!DOCTYPE html>
<!--[if IE 8]><html class="no-js lt-ie9" lang="python" > <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="python" > <!--<![endif]-->
<head>
  <meta charset="utf-8">
  
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  
  <title>QUANTAXIS.QAARP.QARisk &mdash; QUANTAXIS 1.0.39 documentation</title>
  

  
  
  
  

  

  
  
    

  

  
    <link rel="stylesheet" href="../../../_static/css/theme.css" type="text/css" />
  <link rel="stylesheet" href="../../../_static/pygments.css" type="text/css" />
    <link rel="index" title="Index" href="../../../genindex.html" />
    <link rel="search" title="Search" href="../../../search.html" /> 

  
  <script src="../../../_static/js/modernizr.min.js"></script>

</head>

<body class="wy-body-for-nav">

   
  <div class="wy-grid-for-nav">

    
    <nav data-toggle="wy-nav-shift" class="wy-nav-side">
      <div class="wy-side-scroll">
        <div class="wy-side-nav-search">
          

          
            <a href="../../../index.html" class="icon icon-home"> QUANTAXIS
          

          
          </a>

          
            
            
              <div class="version">
                1.0.39
              </div>
            
          

          
<div role="search">
  <form id="rtd-search-form" class="wy-form" action="../../../search.html" method="get">
    <input type="text" name="q" placeholder="Search docs" />
    <input type="hidden" name="check_keywords" value="yes" />
    <input type="hidden" name="area" value="default" />
  </form>
</div>

          
        </div>

        <div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
          
            
            
              
            
            
              <!-- Local TOC -->
              <div class="local-toc"></div>
            
          
        </div>
      </div>
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">

      
      <nav class="wy-nav-top" aria-label="top navigation">
        
          <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
          <a href="../../../index.html">QUANTAXIS</a>
        
      </nav>


      <div class="wy-nav-content">
        
        <div class="rst-content">
        
          















<div role="navigation" aria-label="breadcrumbs navigation">

  <ul class="wy-breadcrumbs">
    
      <li><a href="../../../index.html">Docs</a> &raquo;</li>
        
          <li><a href="../../index.html">Module code</a> &raquo;</li>
        
      <li>QUANTAXIS.QAARP.QARisk</li>
    
    
      <li class="wy-breadcrumbs-aside">
        
      </li>
    
  </ul>

  
  <hr/>
</div>
          <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
           <div itemprop="articleBody">
            
  <h1>Source code for QUANTAXIS.QAARP.QARisk</h1><div class="highlight"><pre>
<span></span><span class="c1"># coding:utf-8</span>
<span class="c1">#</span>
<span class="c1"># The MIT License (MIT)</span>
<span class="c1">#</span>
<span class="c1"># Copyright (c) 2016-2018 yutiansut/QUANTAXIS</span>
<span class="c1">#</span>
<span class="c1"># Permission is hereby granted, free of charge, to any person obtaining a copy</span>
<span class="c1"># of this software and associated documentation files (the &quot;Software&quot;), to deal</span>
<span class="c1"># in the Software without restriction, including without limitation the rights</span>
<span class="c1"># to use, copy, modify, merge, publish, distribute, sublicense, and/or sell</span>
<span class="c1"># copies of the Software, and to permit persons to whom the Software is</span>
<span class="c1"># furnished to do so, subject to the following conditions:</span>
<span class="c1">#</span>
<span class="c1"># The above copyright notice and this permission notice shall be included in all</span>
<span class="c1"># copies or substantial portions of the Software.</span>
<span class="c1">#</span>
<span class="c1"># THE SOFTWARE IS PROVIDED &quot;AS IS&quot;, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR</span>
<span class="c1"># IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,</span>
<span class="c1"># FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE</span>
<span class="c1"># AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER</span>
<span class="c1"># LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,</span>
<span class="c1"># OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE</span>
<span class="c1"># SOFTWARE.</span>


<span class="sd">&quot;&quot;&quot;收益性的包括年化收益率、净利润、总盈利、总亏损、有效年化收益率、资金使用率。</span>

<span class="sd">风险性主要包括胜率、平均盈亏比、最大回撤比例、最大连续亏损次数、最大连续盈利次数、持仓时间占比、贝塔。</span>

<span class="sd">综合性指标主要包括风险收益比，夏普比例，波动率，VAR，偏度，峰度等&quot;&quot;&quot;</span>

<span class="kn">import</span> <span class="nn">math</span>
<span class="kn">import</span> <span class="nn">os</span>
<span class="kn">import</span> <span class="nn">platform</span>
<span class="kn">from</span> <span class="nn">collections</span> <span class="k">import</span> <span class="n">deque</span>
<span class="kn">from</span> <span class="nn">functools</span> <span class="k">import</span> <span class="n">lru_cache</span>
<span class="kn">from</span> <span class="nn">queue</span> <span class="k">import</span> <span class="n">LifoQueue</span>

<span class="kn">import</span> <span class="nn">matplotlib</span>
<span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>
<span class="kn">import</span> <span class="nn">pandas</span> <span class="k">as</span> <span class="nn">pd</span>

<span class="kn">from</span> <span class="nn">QUANTAXIS.QAFetch.QAQuery_Advance</span> <span class="k">import</span> <span class="p">(</span><span class="n">QA_fetch_index_day_adv</span><span class="p">,</span>
                                               <span class="n">QA_fetch_stock_day_adv</span><span class="p">)</span>
<span class="kn">from</span> <span class="nn">QUANTAXIS.QASU.save_account</span> <span class="k">import</span> <span class="n">save_riskanalysis</span>
<span class="kn">from</span> <span class="nn">QUANTAXIS.QAUtil.QADate_trade</span> <span class="k">import</span> <span class="n">QA_util_get_trade_gap</span>
<span class="kn">from</span> <span class="nn">QUANTAXIS.QAUtil.QAParameter</span> <span class="k">import</span> <span class="n">MARKET_TYPE</span>

<span class="c1"># FIXED: no display found</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd">在无GUI的电脑上,会遇到找不到_tkinter的情况 兼容处理</span>
<span class="sd">@尧 2018/05/28</span>
<span class="sd">@喜欢你 @尧 2018/05/29</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="k">if</span> <span class="n">platform</span><span class="o">.</span><span class="n">system</span><span class="p">()</span> <span class="o">!=</span> <span class="s1">&#39;Windows&#39;</span> <span class="ow">and</span> <span class="n">os</span><span class="o">.</span><span class="n">environ</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;DISPLAY&#39;</span><span class="p">,</span> <span class="s1">&#39;&#39;</span><span class="p">)</span> <span class="o">==</span> <span class="s1">&#39;&#39;</span><span class="p">:</span>
    <span class="nb">print</span><span class="p">(</span><span class="s1">&#39;no display found. Using non-interactive Agg backend&#39;</span><span class="p">)</span>
    <span class="nb">print</span><span class="p">(</span><span class="s2">&quot;if you use ssh, you can use ssh with -X parmas to avoid this issue&quot;</span><span class="p">)</span>
    <span class="n">matplotlib</span><span class="o">.</span><span class="n">use</span><span class="p">(</span><span class="s1">&#39;Agg&#39;</span><span class="p">)</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    matplotlib可用模式:</span>
<span class="sd">    [&#39;GTK&#39;, &#39;GTKAgg&#39;, &#39;GTKCairo&#39;, &#39;MacOSX&#39;, &#39;Qt4Agg&#39;, &#39;Qt5Agg&#39;, &#39;TkAgg&#39;, &#39;WX&#39;, </span>
<span class="sd">    &#39;WXAgg&#39;, &#39;GTK3Cairo&#39;, &#39;GTK3Agg&#39;, &#39;WebAgg&#39;, &#39;nbAgg&#39;, &#39;agg&#39;, &#39;cairo&#39;, </span>
<span class="sd">    &#39;gdk&#39;, &#39;pdf&#39;, &#39;pgf&#39;, &#39;ps&#39;, &#39;svg&#39;, &#39;template&#39;]</span>
<span class="sd">    &quot;&quot;&quot;</span>
<span class="k">try</span><span class="p">:</span>
    <span class="kn">import</span> <span class="nn">tkinter</span>
<span class="k">except</span> <span class="n">ModuleNotFoundError</span><span class="p">:</span>
    <span class="sd">&#39;&#39;&#39;</span>
<span class="sd">    ModuleNotFoundError: No module named &#39;tkinter&#39;</span>
<span class="sd">    maybe you should install tk, tcl library</span>
<span class="sd">    &#39;&#39;&#39;</span>
    <span class="nb">print</span><span class="p">(</span><span class="s2">&quot;ModuleNotFoundError: No module named &#39;tkinter&#39;&quot;</span><span class="p">)</span>
    <span class="nb">print</span><span class="p">(</span><span class="s2">&quot;centos 6: sudo yum install tk-devel tcl-devel sqlite-devel gdbm-devel xz-devel readline-devel&quot;</span><span class="p">)</span>
    <span class="nb">print</span><span class="p">(</span><span class="s2">&quot;cnetos 7: sudo yum install tk-devel tcl-devel sqlite-devel gdbm-devel xz-devel readline-devel python3-tk&quot;</span><span class="p">)</span>
    <span class="nb">print</span><span class="p">(</span><span class="s2">&quot;ubuntu: sudo apt install python3-tk&quot;</span><span class="p">)</span>
<span class="k">finally</span><span class="p">:</span>
    <span class="kn">import</span> <span class="nn">matplotlib.patches</span> <span class="k">as</span> <span class="nn">mpatches</span>
    <span class="kn">import</span> <span class="nn">matplotlib.pyplot</span> <span class="k">as</span> <span class="nn">plt</span>
    <span class="kn">import</span> <span class="nn">seaborn</span> <span class="k">as</span> <span class="nn">sns</span>


<div class="viewcode-block" id="QA_Risk"><a class="viewcode-back" href="../../../source/QUANTAXIS.QAARP.html#QUANTAXIS.QAARP.QARisk.QA_Risk">[docs]</a><span class="k">class</span> <span class="nc">QA_Risk</span><span class="p">():</span>
    <span class="sd">&quot;&quot;&quot;QARISK 是一个风险插件</span>

<span class="sd">    需要加载一个account/portfolio类进来:</span>
<span class="sd">    需要有</span>
<span class="sd">    code,start_date,end_date,daily_cash,daily_hold</span>

<span class="sd">    TODO:</span>
<span class="sd">    资金利用率 反应资金的利用程度</span>
<span class="sd">    股票周转率 反应股票的持仓天数</span>
<span class="sd">    预期PNL/统计学PNL</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">account</span><span class="p">,</span> <span class="n">benchmark_code</span><span class="o">=</span><span class="s1">&#39;000300&#39;</span><span class="p">,</span> <span class="n">benchmark_type</span><span class="o">=</span><span class="n">MARKET_TYPE</span><span class="o">.</span><span class="n">INDEX_CN</span><span class="p">,</span> <span class="n">if_fq</span><span class="o">=</span><span class="kc">True</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        if_qf选项是@尧提出的,关于回测的时候成交价格问题(如果按不复权撮合 应该按不复权价格计算assets)</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">account</span> <span class="o">=</span> <span class="n">account</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">benchmark_code</span> <span class="o">=</span> <span class="n">benchmark_code</span>  <span class="c1"># 默认沪深300</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">benchmark_type</span> <span class="o">=</span> <span class="n">benchmark_type</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">fetch</span> <span class="o">=</span> <span class="p">{</span><span class="n">MARKET_TYPE</span><span class="o">.</span><span class="n">STOCK_CN</span><span class="p">:</span> <span class="n">QA_fetch_stock_day_adv</span><span class="p">,</span>
                      <span class="n">MARKET_TYPE</span><span class="o">.</span><span class="n">INDEX_CN</span><span class="p">:</span> <span class="n">QA_fetch_index_day_adv</span><span class="p">}</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">market_data</span> <span class="o">=</span> <span class="n">QA_fetch_stock_day_adv</span><span class="p">(</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">account</span><span class="o">.</span><span class="n">code</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">account</span><span class="o">.</span><span class="n">start_date</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">account</span><span class="o">.</span><span class="n">end_date</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">if_fq</span> <span class="o">=</span> <span class="n">if_fq</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">_assets</span> <span class="o">=</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">market_value</span><span class="o">.</span><span class="n">sum</span><span class="p">(</span>
            <span class="n">axis</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span> <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">account</span><span class="o">.</span><span class="n">daily_cash</span><span class="o">.</span><span class="n">set_index</span><span class="p">(</span><span class="s1">&#39;date&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">cash</span><span class="p">)</span><span class="o">.</span><span class="n">fillna</span><span class="p">(</span><span class="n">method</span><span class="o">=</span><span class="s1">&#39;pad&#39;</span><span class="p">)</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">time_gap</span> <span class="o">=</span> <span class="n">QA_util_get_trade_gap</span><span class="p">(</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">account</span><span class="o">.</span><span class="n">start_date</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">account</span><span class="o">.</span><span class="n">end_date</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">init_cash</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">account</span><span class="o">.</span><span class="n">init_cash</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">init_assets</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">account</span><span class="o">.</span><span class="n">init_assets</span>

    <span class="k">def</span> <span class="nf">__repr__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="s1">&#39;&lt; QA_RISK ANALYSIS ACCOUNT/PORTFOLIO &gt;&#39;</span>

    <span class="k">def</span> <span class="nf">__call__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">([</span><span class="bp">self</span><span class="o">.</span><span class="n">message</span><span class="p">])</span>

    <span class="nd">@property</span>
    <span class="nd">@lru_cache</span><span class="p">()</span>
    <span class="k">def</span> <span class="nf">market_value</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;市值表</span>

<span class="sd">        Returns:</span>
<span class="sd">            pd.DataFrame -- 市值表</span>
<span class="sd">        &quot;&quot;&quot;</span>

        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">if_fq</span><span class="p">:</span>
            <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">market_data</span><span class="o">.</span><span class="n">to_qfq</span><span class="p">()</span><span class="o">.</span><span class="n">pivot</span><span class="p">(</span><span class="s1">&#39;close&#39;</span><span class="p">)</span> <span class="o">*</span> <span class="bp">self</span><span class="o">.</span><span class="n">account</span><span class="o">.</span><span class="n">daily_hold</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">market_data</span><span class="o">.</span><span class="n">pivot</span><span class="p">(</span><span class="s1">&#39;close&#39;</span><span class="p">)</span> <span class="o">*</span> <span class="bp">self</span><span class="o">.</span><span class="n">account</span><span class="o">.</span><span class="n">daily_hold</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">assets</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="n">x1</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_assets</span><span class="o">.</span><span class="n">reset_index</span><span class="p">()</span>
        <span class="k">return</span> <span class="n">x1</span><span class="o">.</span><span class="n">assign</span><span class="p">(</span><span class="n">date</span><span class="o">=</span><span class="n">pd</span><span class="o">.</span><span class="n">to_datetime</span><span class="p">(</span><span class="n">x1</span><span class="o">.</span><span class="n">date</span><span class="p">))</span><span class="o">.</span><span class="n">set_index</span><span class="p">(</span><span class="s1">&#39;date&#39;</span><span class="p">)[</span><span class="mi">0</span><span class="p">]</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">max_dropback</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;最大回撤</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="nb">round</span><span class="p">(</span><span class="nb">float</span><span class="p">(</span><span class="nb">max</span><span class="p">([(</span><span class="bp">self</span><span class="o">.</span><span class="n">assets</span><span class="o">.</span><span class="n">iloc</span><span class="p">[</span><span class="n">idx</span><span class="p">]</span> <span class="o">-</span> <span class="bp">self</span><span class="o">.</span><span class="n">assets</span><span class="o">.</span><span class="n">iloc</span><span class="p">[</span><span class="n">idx</span><span class="p">::]</span><span class="o">.</span><span class="n">min</span><span class="p">())</span><span class="o">/</span><span class="bp">self</span><span class="o">.</span><span class="n">assets</span><span class="o">.</span><span class="n">iloc</span><span class="p">[</span><span class="n">idx</span><span class="p">]</span> <span class="k">for</span> <span class="n">idx</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">assets</span><span class="p">))])),</span> <span class="mi">2</span><span class="p">)</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">total_commission</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;总手续费</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="o">-</span><span class="nb">abs</span><span class="p">(</span><span class="nb">round</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">account</span><span class="o">.</span><span class="n">history_table</span><span class="o">.</span><span class="n">commission</span><span class="o">.</span><span class="n">sum</span><span class="p">(),</span> <span class="mi">2</span><span class="p">))</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">total_tax</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;总印花税</span>

<span class="sd">        &quot;&quot;&quot;</span>

        <span class="k">return</span> <span class="o">-</span><span class="nb">abs</span><span class="p">(</span><span class="nb">round</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">account</span><span class="o">.</span><span class="n">history_table</span><span class="o">.</span><span class="n">tax</span><span class="o">.</span><span class="n">sum</span><span class="p">(),</span> <span class="mi">2</span><span class="p">))</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">profit_construct</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;利润构成</span>

<span class="sd">        Returns:</span>
<span class="sd">            dict -- 利润构成表</span>
<span class="sd">        &quot;&quot;&quot;</span>

        <span class="k">return</span> <span class="p">{</span>
            <span class="s1">&#39;total_buyandsell&#39;</span><span class="p">:</span> <span class="nb">round</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">profit_money</span><span class="o">-</span><span class="bp">self</span><span class="o">.</span><span class="n">total_commission</span><span class="o">-</span><span class="bp">self</span><span class="o">.</span><span class="n">total_tax</span><span class="p">,</span> <span class="mi">2</span><span class="p">),</span>
            <span class="s1">&#39;total_tax&#39;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">total_tax</span><span class="p">,</span>
            <span class="s1">&#39;total_commission&#39;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">total_commission</span><span class="p">,</span>
            <span class="s1">&#39;total_profit&#39;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">profit_money</span>
        <span class="p">}</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">profit_money</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;盈利额</span>

<span class="sd">        Returns:</span>
<span class="sd">            [type] -- [description]</span>
<span class="sd">        &quot;&quot;&quot;</span>

        <span class="k">return</span> <span class="nb">round</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">assets</span><span class="o">.</span><span class="n">iloc</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span><span class="o">-</span><span class="bp">self</span><span class="o">.</span><span class="n">init_cash</span><span class="p">,</span> <span class="mi">2</span><span class="p">)</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">profit</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;盈利率(百分比)</span>

<span class="sd">        Returns:</span>
<span class="sd">            [type] -- [description]</span>
<span class="sd">        &quot;&quot;&quot;</span>

        <span class="k">return</span> <span class="nb">round</span><span class="p">(</span><span class="nb">float</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">calc_profit</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">assets</span><span class="p">)),</span> <span class="mi">2</span><span class="p">)</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">profit_pct</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;利润</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">calc_profitpctchange</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">assets</span><span class="p">)</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">annualize_return</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;年化收益</span>

<span class="sd">        Returns:</span>
<span class="sd">            [type] -- [description]</span>
<span class="sd">        &quot;&quot;&quot;</span>

        <span class="k">return</span> <span class="nb">round</span><span class="p">(</span><span class="nb">float</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">calc_annualize_return</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">assets</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">time_gap</span><span class="p">)),</span> <span class="mi">2</span><span class="p">)</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">volatility</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;波动率</span>

<span class="sd">        Returns:</span>
<span class="sd">            [type] -- [description]</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="nb">round</span><span class="p">(</span><span class="nb">float</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">profit_pct</span><span class="o">.</span><span class="n">std</span><span class="p">()</span> <span class="o">*</span> <span class="n">math</span><span class="o">.</span><span class="n">sqrt</span><span class="p">(</span><span class="mi">250</span><span class="p">)),</span> <span class="mi">2</span><span class="p">)</span>

    <span class="nd">@property</span>
    <span class="nd">@lru_cache</span><span class="p">()</span>
    <span class="k">def</span> <span class="nf">message</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="p">{</span>
            <span class="s1">&#39;account_cookie&#39;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">account</span><span class="o">.</span><span class="n">account_cookie</span><span class="p">,</span>
            <span class="s1">&#39;portfolio_cookie&#39;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">account</span><span class="o">.</span><span class="n">portfolio_cookie</span><span class="p">,</span>
            <span class="s1">&#39;user_cookie&#39;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">account</span><span class="o">.</span><span class="n">user_cookie</span><span class="p">,</span>
            <span class="s1">&#39;annualize_return&#39;</span><span class="p">:</span> <span class="nb">round</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">annualize_return</span><span class="p">,</span> <span class="mi">2</span><span class="p">),</span>
            <span class="s1">&#39;profit&#39;</span><span class="p">:</span> <span class="nb">round</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">profit</span><span class="p">,</span> <span class="mi">2</span><span class="p">),</span>
            <span class="s1">&#39;max_dropback&#39;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">max_dropback</span><span class="p">,</span>
            <span class="s1">&#39;time_gap&#39;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">time_gap</span><span class="p">,</span>
            <span class="s1">&#39;volatility&#39;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">volatility</span><span class="p">,</span>
            <span class="s1">&#39;benchmark_code&#39;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">benchmark_code</span><span class="p">,</span>
            <span class="s1">&#39;bm_annualizereturn&#39;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">benchmark_annualize_return</span><span class="p">,</span>
            <span class="s1">&#39;bn_profit&#39;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">benchmark_profit</span><span class="p">,</span>
            <span class="s1">&#39;beta&#39;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">beta</span><span class="p">,</span>
            <span class="s1">&#39;alpha&#39;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">alpha</span><span class="p">,</span>
            <span class="s1">&#39;sharpe&#39;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">sharpe</span><span class="p">,</span>
            <span class="s1">&#39;init_cash&#39;</span><span class="p">:</span> <span class="s2">&quot;</span><span class="si">%0.2f</span><span class="s2">&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="nb">float</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">init_cash</span><span class="p">)),</span>
            <span class="s1">&#39;last_assets&#39;</span><span class="p">:</span> <span class="s2">&quot;</span><span class="si">%0.2f</span><span class="s2">&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="nb">float</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">assets</span><span class="o">.</span><span class="n">iloc</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">]))</span>

            <span class="c1">#&#39;init_assets&#39;: round(float(self.init_assets), 2),</span>
            <span class="c1">#&#39;last_assets&#39;: round(float(self.assets.iloc[-1]), 2)</span>
        <span class="p">}</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">benchmark_data</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        基准组合的行情数据(一般是组合,可以调整)</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">fetch</span><span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">benchmark_type</span><span class="p">](</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">benchmark_code</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">account</span><span class="o">.</span><span class="n">start_date</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">account</span><span class="o">.</span><span class="n">end_date</span><span class="p">)</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">benchmark_assets</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        基准组合的账户资产队列</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">benchmark_data</span><span class="o">.</span><span class="n">close</span> <span class="o">/</span> <span class="nb">float</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">benchmark_data</span><span class="o">.</span><span class="n">open</span><span class="o">.</span><span class="n">iloc</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span> <span class="o">*</span> <span class="nb">float</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">init_cash</span><span class="p">))</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">benchmark_profit</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        基准组合的收益</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="nb">round</span><span class="p">(</span><span class="nb">float</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">calc_profit</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">benchmark_assets</span><span class="p">)),</span> <span class="mi">2</span><span class="p">)</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">benchmark_annualize_return</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;基准组合的年化收益</span>

<span class="sd">        Returns:</span>
<span class="sd">            [type] -- [description]</span>
<span class="sd">        &quot;&quot;&quot;</span>

        <span class="k">return</span> <span class="nb">round</span><span class="p">(</span><span class="nb">float</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">calc_annualize_return</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">benchmark_assets</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">time_gap</span><span class="p">)),</span> <span class="mi">2</span><span class="p">)</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">benchmark_profitpct</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        benchmark 基准组合的收益百分比计算</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">calc_profitpctchange</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">benchmark_assets</span><span class="p">)</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">beta</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        beta比率 组合的系统性风险</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="nb">round</span><span class="p">(</span><span class="nb">float</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">calc_beta</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">profit_pct</span><span class="o">.</span><span class="n">dropna</span><span class="p">(),</span> <span class="bp">self</span><span class="o">.</span><span class="n">benchmark_profitpct</span><span class="o">.</span><span class="n">dropna</span><span class="p">())),</span> <span class="mi">2</span><span class="p">)</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">alpha</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        alpha比率 与市场基准收益无关的超额收益率</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="nb">round</span><span class="p">(</span><span class="nb">float</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">calc_alpha</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">annualize_return</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">benchmark_annualize_return</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">beta</span><span class="p">,</span> <span class="mf">0.05</span><span class="p">)),</span> <span class="mi">2</span><span class="p">)</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">sharpe</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        夏普比率</span>

<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="nb">round</span><span class="p">(</span><span class="nb">float</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">calc_sharpe</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">annualize_return</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">volatility</span><span class="p">,</span> <span class="mf">0.05</span><span class="p">)),</span> <span class="mi">2</span><span class="p">)</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">sortino</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot; </span>
<span class="sd">        索提诺比率 投资组合收益和下行风险比值</span>

<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">pass</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">calmar</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        卡玛比率</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">pass</span>

<div class="viewcode-block" id="QA_Risk.set_benchmark"><a class="viewcode-back" href="../../../source/QUANTAXIS.QAARP.html#QUANTAXIS.QAARP.QARisk.QA_Risk.set_benchmark">[docs]</a>    <span class="k">def</span> <span class="nf">set_benchmark</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">code</span><span class="p">,</span> <span class="n">market_type</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">benchmark_code</span> <span class="o">=</span> <span class="n">code</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">benchmark_type</span> <span class="o">=</span> <span class="n">market_type</span></div>

<div class="viewcode-block" id="QA_Risk.calc_annualize_return"><a class="viewcode-back" href="../../../source/QUANTAXIS.QAARP.html#QUANTAXIS.QAARP.QARisk.QA_Risk.calc_annualize_return">[docs]</a>    <span class="k">def</span> <span class="nf">calc_annualize_return</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">assets</span><span class="p">,</span> <span class="n">days</span><span class="p">):</span>
        <span class="k">return</span> <span class="nb">round</span><span class="p">((</span><span class="nb">float</span><span class="p">(</span><span class="n">assets</span><span class="o">.</span><span class="n">iloc</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">])</span> <span class="o">/</span> <span class="nb">float</span><span class="p">(</span><span class="n">assets</span><span class="o">.</span><span class="n">iloc</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span> <span class="o">-</span> <span class="mi">1</span><span class="p">)</span><span class="o">/</span><span class="p">(</span><span class="nb">float</span><span class="p">(</span><span class="n">days</span><span class="p">)</span> <span class="o">/</span> <span class="mi">250</span><span class="p">),</span> <span class="mi">2</span><span class="p">)</span></div>

<div class="viewcode-block" id="QA_Risk.calc_profitpctchange"><a class="viewcode-back" href="../../../source/QUANTAXIS.QAARP.html#QUANTAXIS.QAARP.QARisk.QA_Risk.calc_profitpctchange">[docs]</a>    <span class="k">def</span> <span class="nf">calc_profitpctchange</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">assets</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">assets</span><span class="p">[::</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span><span class="o">.</span><span class="n">pct_change</span><span class="p">()</span></div>

<div class="viewcode-block" id="QA_Risk.calc_beta"><a class="viewcode-back" href="../../../source/QUANTAXIS.QAARP.html#QUANTAXIS.QAARP.QARisk.QA_Risk.calc_beta">[docs]</a>    <span class="k">def</span> <span class="nf">calc_beta</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">assest_profit</span><span class="p">,</span> <span class="n">benchmark_profit</span><span class="p">):</span>

        <span class="n">calc_cov</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">cov</span><span class="p">(</span><span class="n">assest_profit</span><span class="p">,</span> <span class="n">benchmark_profit</span><span class="p">)</span>
        <span class="n">beta</span> <span class="o">=</span> <span class="n">calc_cov</span><span class="p">[</span><span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">]</span> <span class="o">/</span> <span class="n">calc_cov</span><span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">]</span>
        <span class="k">return</span> <span class="n">beta</span></div>

<div class="viewcode-block" id="QA_Risk.calc_alpha"><a class="viewcode-back" href="../../../source/QUANTAXIS.QAARP.html#QUANTAXIS.QAARP.QARisk.QA_Risk.calc_alpha">[docs]</a>    <span class="k">def</span> <span class="nf">calc_alpha</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">annualized_returns</span><span class="p">,</span> <span class="n">benchmark_annualized_returns</span><span class="p">,</span> <span class="n">beta</span><span class="p">,</span> <span class="n">r</span><span class="o">=</span><span class="mf">0.05</span><span class="p">):</span>

        <span class="n">alpha</span> <span class="o">=</span> <span class="p">(</span><span class="n">annualized_returns</span> <span class="o">-</span> <span class="n">r</span><span class="p">)</span> <span class="o">-</span> <span class="p">(</span><span class="n">beta</span><span class="p">)</span> <span class="o">*</span> \
            <span class="p">(</span><span class="n">benchmark_annualized_returns</span> <span class="o">-</span> <span class="n">r</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">alpha</span></div>

<div class="viewcode-block" id="QA_Risk.calc_profit"><a class="viewcode-back" href="../../../source/QUANTAXIS.QAARP.html#QUANTAXIS.QAARP.QARisk.QA_Risk.calc_profit">[docs]</a>    <span class="k">def</span> <span class="nf">calc_profit</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">assets</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        计算账户收益</span>
<span class="sd">        期末资产/期初资产 -1</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="p">(</span><span class="nb">float</span><span class="p">(</span><span class="n">assets</span><span class="o">.</span><span class="n">iloc</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">])</span> <span class="o">/</span> <span class="nb">float</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">init_cash</span><span class="p">))</span> <span class="o">-</span> <span class="mi">1</span></div>

<div class="viewcode-block" id="QA_Risk.calc_sharpe"><a class="viewcode-back" href="../../../source/QUANTAXIS.QAARP.html#QUANTAXIS.QAARP.QARisk.QA_Risk.calc_sharpe">[docs]</a>    <span class="k">def</span> <span class="nf">calc_sharpe</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">annualized_returns</span><span class="p">,</span> <span class="n">volatility_year</span><span class="p">,</span> <span class="n">r</span><span class="o">=</span><span class="mf">0.05</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        计算夏普比率</span>
<span class="sd">        r是无风险收益</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="c1"># 会出现0</span>
        <span class="k">if</span> <span class="n">volatility_year</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
            <span class="k">return</span> <span class="mi">0</span>
        <span class="k">return</span> <span class="p">(</span><span class="n">annualized_returns</span> <span class="o">-</span> <span class="n">r</span><span class="p">)</span> <span class="o">/</span> <span class="n">volatility_year</span></div>

<div class="viewcode-block" id="QA_Risk.save"><a class="viewcode-back" href="../../../source/QUANTAXIS.QAARP.html#QUANTAXIS.QAARP.QARisk.QA_Risk.save">[docs]</a>    <span class="k">def</span> <span class="nf">save</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;save to mongodb</span>

<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">save_riskanalysis</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">message</span><span class="p">)</span></div>

<div class="viewcode-block" id="QA_Risk.plot_assets_curve"><a class="viewcode-back" href="../../../source/QUANTAXIS.QAARP.html#QUANTAXIS.QAARP.QARisk.QA_Risk.plot_assets_curve">[docs]</a>    <span class="k">def</span> <span class="nf">plot_assets_curve</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">length</span><span class="o">=</span><span class="mi">14</span><span class="p">,</span> <span class="n">height</span><span class="o">=</span><span class="mi">12</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        资金曲线叠加图</span>
<span class="sd">        @Roy T.Burns 2018/05/29 修改百分比显示错误</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">plt</span><span class="o">.</span><span class="n">style</span><span class="o">.</span><span class="n">use</span><span class="p">(</span><span class="s1">&#39;ggplot&#39;</span><span class="p">)</span>
        <span class="n">plt</span><span class="o">.</span><span class="n">figure</span><span class="p">(</span><span class="n">figsize</span><span class="o">=</span><span class="p">(</span><span class="n">length</span><span class="p">,</span> <span class="n">height</span><span class="p">))</span>
        <span class="n">plt</span><span class="o">.</span><span class="n">subplot</span><span class="p">(</span><span class="mi">211</span><span class="p">)</span>
        <span class="n">plt</span><span class="o">.</span><span class="n">title</span><span class="p">(</span><span class="s1">&#39;BASIC INFO&#39;</span><span class="p">,</span> <span class="n">fontsize</span><span class="o">=</span><span class="mi">12</span><span class="p">)</span>
        <span class="n">plt</span><span class="o">.</span><span class="n">axis</span><span class="p">([</span><span class="mi">0</span><span class="p">,</span> <span class="n">length</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mf">0.6</span><span class="p">])</span>
        <span class="n">plt</span><span class="o">.</span><span class="n">axis</span><span class="p">(</span><span class="s1">&#39;off&#39;</span><span class="p">)</span>
        <span class="n">i</span> <span class="o">=</span> <span class="mi">0</span>
        <span class="k">for</span> <span class="n">item</span> <span class="ow">in</span> <span class="p">[</span><span class="s1">&#39;account_cookie&#39;</span><span class="p">,</span> <span class="s1">&#39;portfolio_cookie&#39;</span><span class="p">,</span> <span class="s1">&#39;user_cookie&#39;</span><span class="p">]:</span>
            <span class="n">plt</span><span class="o">.</span><span class="n">text</span><span class="p">(</span><span class="n">i</span><span class="p">,</span> <span class="mf">0.5</span><span class="p">,</span> <span class="s1">&#39;</span><span class="si">{}</span><span class="s1"> : </span><span class="si">{}</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span>
                <span class="n">item</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">message</span><span class="p">[</span><span class="n">item</span><span class="p">]),</span> <span class="n">fontsize</span><span class="o">=</span><span class="mi">10</span><span class="p">,</span> <span class="n">rotation</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span> <span class="n">wrap</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
            <span class="n">i</span> <span class="o">+=</span> <span class="p">(</span><span class="n">length</span><span class="o">/</span><span class="mf">2.8</span><span class="p">)</span>
        <span class="n">i</span> <span class="o">=</span> <span class="mi">0</span>
        <span class="k">for</span> <span class="n">item</span> <span class="ow">in</span> <span class="p">[</span><span class="s1">&#39;benchmark_code&#39;</span><span class="p">,</span> <span class="s1">&#39;time_gap&#39;</span><span class="p">,</span> <span class="s1">&#39;max_dropback&#39;</span><span class="p">]:</span>
            <span class="n">plt</span><span class="o">.</span><span class="n">text</span><span class="p">(</span><span class="n">i</span><span class="p">,</span> <span class="mf">0.4</span><span class="p">,</span> <span class="s1">&#39;</span><span class="si">{}</span><span class="s1"> : </span><span class="si">{}</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span>
                <span class="n">item</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">message</span><span class="p">[</span><span class="n">item</span><span class="p">]),</span> <span class="n">fontsize</span><span class="o">=</span><span class="mi">10</span><span class="p">,</span> <span class="n">ha</span><span class="o">=</span><span class="s1">&#39;left&#39;</span><span class="p">,</span> <span class="n">rotation</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span> <span class="n">wrap</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
            <span class="n">i</span> <span class="o">+=</span> <span class="p">(</span><span class="n">length</span><span class="o">/</span><span class="mf">2.8</span><span class="p">)</span>
        <span class="n">i</span> <span class="o">=</span> <span class="mi">0</span>
        <span class="k">for</span> <span class="n">item</span> <span class="ow">in</span> <span class="p">[</span><span class="s1">&#39;annualize_return&#39;</span><span class="p">,</span> <span class="s1">&#39;bm_annualizereturn&#39;</span><span class="p">,</span> <span class="s1">&#39;profit&#39;</span><span class="p">]:</span>
            <span class="n">plt</span><span class="o">.</span><span class="n">text</span><span class="p">(</span><span class="n">i</span><span class="p">,</span> <span class="mf">0.3</span><span class="p">,</span> <span class="s1">&#39;</span><span class="si">{}</span><span class="s1"> : </span><span class="si">{}</span><span class="s1"> %&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">item</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">message</span><span class="o">.</span><span class="n">get</span><span class="p">(</span>
                <span class="n">item</span><span class="p">,</span> <span class="mi">0</span><span class="p">)</span><span class="o">*</span><span class="mi">100</span><span class="p">),</span> <span class="n">fontsize</span><span class="o">=</span><span class="mi">10</span><span class="p">,</span> <span class="n">ha</span><span class="o">=</span><span class="s1">&#39;left&#39;</span><span class="p">,</span> <span class="n">rotation</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span> <span class="n">wrap</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
            <span class="n">i</span> <span class="o">+=</span> <span class="n">length</span><span class="o">/</span><span class="mf">2.8</span>
        <span class="n">i</span> <span class="o">=</span> <span class="mi">0</span>
        <span class="k">for</span> <span class="n">item</span> <span class="ow">in</span> <span class="p">[</span><span class="s1">&#39;init_cash&#39;</span><span class="p">,</span> <span class="s1">&#39;last_assets&#39;</span><span class="p">,</span> <span class="s1">&#39;volatility&#39;</span><span class="p">]:</span>
            <span class="n">plt</span><span class="o">.</span><span class="n">text</span><span class="p">(</span><span class="n">i</span><span class="p">,</span> <span class="mf">0.2</span><span class="p">,</span> <span class="s1">&#39;</span><span class="si">{}</span><span class="s1"> : </span><span class="si">{}</span><span class="s1"> &#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span>
                <span class="n">item</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">message</span><span class="p">[</span><span class="n">item</span><span class="p">]),</span> <span class="n">fontsize</span><span class="o">=</span><span class="mi">10</span><span class="p">,</span> <span class="n">ha</span><span class="o">=</span><span class="s1">&#39;left&#39;</span><span class="p">,</span> <span class="n">rotation</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span> <span class="n">wrap</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
            <span class="n">i</span> <span class="o">+=</span> <span class="n">length</span><span class="o">/</span><span class="mf">2.8</span>
        <span class="n">i</span> <span class="o">=</span> <span class="mi">0</span>
        <span class="k">for</span> <span class="n">item</span> <span class="ow">in</span> <span class="p">[</span><span class="s1">&#39;alpha&#39;</span><span class="p">,</span> <span class="s1">&#39;beta&#39;</span><span class="p">,</span> <span class="s1">&#39;sharpe&#39;</span><span class="p">]:</span>
            <span class="n">plt</span><span class="o">.</span><span class="n">text</span><span class="p">(</span><span class="n">i</span><span class="p">,</span> <span class="mf">0.1</span><span class="p">,</span> <span class="s1">&#39;</span><span class="si">{}</span><span class="s1"> : </span><span class="si">{}</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span>
                <span class="n">item</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">message</span><span class="p">[</span><span class="n">item</span><span class="p">]),</span> <span class="n">ha</span><span class="o">=</span><span class="s1">&#39;left&#39;</span><span class="p">,</span> <span class="n">fontsize</span><span class="o">=</span><span class="mi">10</span><span class="p">,</span> <span class="n">rotation</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span> <span class="n">wrap</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
            <span class="n">i</span> <span class="o">+=</span> <span class="n">length</span><span class="o">/</span><span class="mf">2.8</span>
        <span class="n">plt</span><span class="o">.</span><span class="n">subplot</span><span class="p">(</span><span class="mi">212</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">assets</span><span class="o">.</span><span class="n">plot</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">benchmark_assets</span><span class="o">.</span><span class="n">xs</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">benchmark_code</span><span class="p">,</span> <span class="n">level</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span><span class="o">.</span><span class="n">plot</span><span class="p">()</span>

        <span class="n">asset_p</span> <span class="o">=</span> <span class="n">mpatches</span><span class="o">.</span><span class="n">Patch</span><span class="p">(</span>
            <span class="n">color</span><span class="o">=</span><span class="s1">&#39;red&#39;</span><span class="p">,</span> <span class="n">label</span><span class="o">=</span><span class="s1">&#39;</span><span class="si">{}</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">account</span><span class="o">.</span><span class="n">account_cookie</span><span class="p">))</span>
        <span class="n">asset_b</span> <span class="o">=</span> <span class="n">mpatches</span><span class="o">.</span><span class="n">Patch</span><span class="p">(</span>
            <span class="n">label</span><span class="o">=</span><span class="s1">&#39;benchmark </span><span class="si">{}</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">benchmark_code</span><span class="p">))</span>
        <span class="n">plt</span><span class="o">.</span><span class="n">legend</span><span class="p">(</span><span class="n">handles</span><span class="o">=</span><span class="p">[</span><span class="n">asset_p</span><span class="p">,</span> <span class="n">asset_b</span><span class="p">],</span> <span class="n">loc</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span>
        <span class="n">plt</span><span class="o">.</span><span class="n">title</span><span class="p">(</span><span class="s1">&#39;ASSET AND BENCKMARK&#39;</span><span class="p">)</span>
        <span class="n">plt</span><span class="o">.</span><span class="n">show</span><span class="p">()</span></div>

<div class="viewcode-block" id="QA_Risk.plot_dailyhold"><a class="viewcode-back" href="../../../source/QUANTAXIS.QAARP.html#QUANTAXIS.QAARP.QARisk.QA_Risk.plot_dailyhold">[docs]</a>    <span class="k">def</span> <span class="nf">plot_dailyhold</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">start</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">end</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        使用热力图画出每日持仓</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">start</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">account</span><span class="o">.</span><span class="n">start_date</span> <span class="k">if</span> <span class="n">start</span> <span class="ow">is</span> <span class="kc">None</span> <span class="k">else</span> <span class="n">start</span>
        <span class="n">end</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">account</span><span class="o">.</span><span class="n">end_date</span> <span class="k">if</span> <span class="n">end</span> <span class="ow">is</span> <span class="kc">None</span> <span class="k">else</span> <span class="n">end</span>
        <span class="n">_</span><span class="p">,</span> <span class="n">ax</span> <span class="o">=</span> <span class="n">plt</span><span class="o">.</span><span class="n">subplots</span><span class="p">(</span><span class="n">figsize</span><span class="o">=</span><span class="p">(</span><span class="mi">20</span><span class="p">,</span> <span class="mi">8</span><span class="p">))</span>
        <span class="n">sns</span><span class="o">.</span><span class="n">heatmap</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">account</span><span class="o">.</span><span class="n">daily_hold</span><span class="o">.</span><span class="n">reset_index</span><span class="p">()</span><span class="o">.</span><span class="n">drop</span><span class="p">(</span><span class="s1">&#39;account_cookie&#39;</span><span class="p">,</span> <span class="n">axis</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span><span class="o">.</span><span class="n">set_index</span><span class="p">(</span>
            <span class="s1">&#39;date&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">loc</span><span class="p">[</span><span class="n">start</span><span class="p">:</span><span class="n">end</span><span class="p">],</span> <span class="n">cmap</span><span class="o">=</span><span class="s2">&quot;YlGnBu&quot;</span><span class="p">,</span> <span class="n">linewidths</span><span class="o">=</span><span class="mf">0.05</span><span class="p">,</span> <span class="n">ax</span><span class="o">=</span><span class="n">ax</span><span class="p">)</span>
        <span class="n">ax</span><span class="o">.</span><span class="n">set_title</span><span class="p">(</span>
            <span class="s1">&#39;HOLD TABLE --ACCOUNT: </span><span class="si">{}</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">account</span><span class="o">.</span><span class="n">account_cookie</span><span class="p">))</span>
        <span class="n">ax</span><span class="o">.</span><span class="n">set_xlabel</span><span class="p">(</span><span class="s1">&#39;Code&#39;</span><span class="p">)</span>
        <span class="n">ax</span><span class="o">.</span><span class="n">set_ylabel</span><span class="p">(</span><span class="s1">&#39;DATETIME&#39;</span><span class="p">)</span>
        <span class="n">plt</span><span class="o">.</span><span class="n">show</span><span class="p">()</span></div>

<div class="viewcode-block" id="QA_Risk.plot_signal"><a class="viewcode-back" href="../../../source/QUANTAXIS.QAARP.html#QUANTAXIS.QAARP.QARisk.QA_Risk.plot_signal">[docs]</a>    <span class="k">def</span> <span class="nf">plot_signal</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">start</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">end</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        使用热力图画出买卖信号</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">start</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">account</span><span class="o">.</span><span class="n">start_date</span> <span class="k">if</span> <span class="n">start</span> <span class="ow">is</span> <span class="kc">None</span> <span class="k">else</span> <span class="n">start</span>
        <span class="n">end</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">account</span><span class="o">.</span><span class="n">end_date</span> <span class="k">if</span> <span class="n">end</span> <span class="ow">is</span> <span class="kc">None</span> <span class="k">else</span> <span class="n">end</span>
        <span class="n">_</span><span class="p">,</span> <span class="n">ax</span> <span class="o">=</span> <span class="n">plt</span><span class="o">.</span><span class="n">subplots</span><span class="p">(</span><span class="n">figsize</span><span class="o">=</span><span class="p">(</span><span class="mi">20</span><span class="p">,</span> <span class="mi">18</span><span class="p">))</span>
        <span class="n">sns</span><span class="o">.</span><span class="n">heatmap</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">account</span><span class="o">.</span><span class="n">trade</span><span class="o">.</span><span class="n">reset_index</span><span class="p">()</span><span class="o">.</span><span class="n">drop</span><span class="p">(</span><span class="s1">&#39;account_cookie&#39;</span><span class="p">,</span> <span class="n">axis</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span><span class="o">.</span><span class="n">set_index</span><span class="p">(</span>
            <span class="s1">&#39;datetime&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">loc</span><span class="p">[</span><span class="n">start</span><span class="p">:</span><span class="n">end</span><span class="p">],</span> <span class="n">cmap</span><span class="o">=</span><span class="s2">&quot;YlGnBu&quot;</span><span class="p">,</span> <span class="n">linewidths</span><span class="o">=</span><span class="mf">0.05</span><span class="p">,</span> <span class="n">ax</span><span class="o">=</span><span class="n">ax</span><span class="p">)</span>
        <span class="n">ax</span><span class="o">.</span><span class="n">set_title</span><span class="p">(</span>
            <span class="s1">&#39;SIGNAL TABLE --ACCOUNT: </span><span class="si">{}</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">account</span><span class="o">.</span><span class="n">account_cookie</span><span class="p">))</span>
        <span class="n">ax</span><span class="o">.</span><span class="n">set_xlabel</span><span class="p">(</span><span class="s1">&#39;Code&#39;</span><span class="p">)</span>
        <span class="n">ax</span><span class="o">.</span><span class="n">set_ylabel</span><span class="p">(</span><span class="s1">&#39;DATETIME&#39;</span><span class="p">)</span>
        <span class="n">plt</span><span class="o">.</span><span class="n">show</span><span class="p">()</span></div>

<div class="viewcode-block" id="QA_Risk.generate_plots"><a class="viewcode-back" href="../../../source/QUANTAXIS.QAARP.html#QUANTAXIS.QAARP.QARisk.QA_Risk.generate_plots">[docs]</a>    <span class="k">def</span> <span class="nf">generate_plots</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        生成图像</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">plot_assets_curve</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">plot_dailyhold</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">plot_signal</span><span class="p">()</span></div></div>


<div class="viewcode-block" id="QA_Performance"><a class="viewcode-back" href="../../../source/QUANTAXIS.QAARP.html#QUANTAXIS.QAARP.QARisk.QA_Performance">[docs]</a><span class="k">class</span> <span class="nc">QA_Performance</span><span class="p">():</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    QA_Performance是一个绩效分析插件</span>

<span class="sd">    需要加载一个account/portfolio类进来:</span>
<span class="sd">    需要有</span>
<span class="sd">    code,start_date,end_date,daily_cash,daily_hold</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">account</span><span class="p">):</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">account</span> <span class="o">=</span> <span class="n">account</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_style_title</span> <span class="o">=</span> <span class="p">[</span><span class="s1">&#39;beta&#39;</span><span class="p">,</span> <span class="s1">&#39;momentum&#39;</span><span class="p">,</span> <span class="s1">&#39;size&#39;</span><span class="p">,</span> <span class="s1">&#39;earning_yield&#39;</span><span class="p">,</span>
                             <span class="s1">&#39;volatility&#39;</span><span class="p">,</span> <span class="s1">&#39;growth&#39;</span><span class="p">,</span> <span class="s1">&#39;value&#39;</span><span class="p">,</span> <span class="s1">&#39;leverage&#39;</span><span class="p">,</span> <span class="s1">&#39;liquidity&#39;</span><span class="p">,</span> <span class="s1">&#39;reversal&#39;</span><span class="p">]</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">prefer</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">pass</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">style</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;风格分析</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">pass</span>

    <span class="nd">@property</span>
    <span class="nd">@lru_cache</span><span class="p">()</span>
    <span class="k">def</span> <span class="nf">pnl_lifo</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        使用后进先出法配对成交记录</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">X</span> <span class="o">=</span> <span class="nb">dict</span><span class="p">(</span><span class="nb">zip</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">account</span><span class="o">.</span><span class="n">code</span><span class="p">,</span> <span class="p">[</span><span class="n">LifoQueue</span><span class="p">()</span>
                                         <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">account</span><span class="o">.</span><span class="n">code</span><span class="p">))]))</span>
        <span class="n">pair_table</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="k">for</span> <span class="n">_</span><span class="p">,</span> <span class="n">data</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">account</span><span class="o">.</span><span class="n">history_table</span><span class="o">.</span><span class="n">iterrows</span><span class="p">():</span>
            <span class="k">if</span> <span class="n">data</span><span class="o">.</span><span class="n">amount</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">:</span>
                <span class="n">X</span><span class="p">[</span><span class="n">data</span><span class="o">.</span><span class="n">code</span><span class="p">]</span><span class="o">.</span><span class="n">put</span><span class="p">((</span><span class="n">data</span><span class="o">.</span><span class="n">datetime</span><span class="p">,</span> <span class="n">data</span><span class="o">.</span><span class="n">amount</span><span class="p">,</span> <span class="n">data</span><span class="o">.</span><span class="n">price</span><span class="p">))</span>
            <span class="k">elif</span> <span class="n">data</span><span class="o">.</span><span class="n">amount</span> <span class="o">&lt;</span> <span class="mi">0</span><span class="p">:</span>
                <span class="k">while</span> <span class="kc">True</span><span class="p">:</span>
                    <span class="n">l</span> <span class="o">=</span> <span class="n">X</span><span class="p">[</span><span class="n">data</span><span class="o">.</span><span class="n">code</span><span class="p">]</span><span class="o">.</span><span class="n">get</span><span class="p">()</span>
                    <span class="k">if</span> <span class="n">l</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">==</span> <span class="nb">abs</span><span class="p">(</span><span class="n">data</span><span class="o">.</span><span class="n">amount</span><span class="p">):</span>
                        <span class="n">pair_table</span><span class="o">.</span><span class="n">append</span><span class="p">(</span>
                            <span class="p">[</span><span class="n">data</span><span class="o">.</span><span class="n">code</span><span class="p">,</span> <span class="n">data</span><span class="o">.</span><span class="n">datetime</span><span class="p">,</span> <span class="n">l</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="nb">abs</span><span class="p">(</span><span class="n">data</span><span class="o">.</span><span class="n">amount</span><span class="p">),</span> <span class="n">data</span><span class="o">.</span><span class="n">price</span><span class="p">,</span> <span class="n">l</span><span class="p">[</span><span class="mi">2</span><span class="p">]])</span>
                        <span class="k">break</span>
                    <span class="k">if</span> <span class="n">l</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">&gt;</span> <span class="nb">abs</span><span class="p">(</span><span class="n">data</span><span class="o">.</span><span class="n">amount</span><span class="p">):</span>
                        <span class="n">temp</span> <span class="o">=</span> <span class="p">(</span><span class="n">l</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="n">l</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span><span class="o">+</span><span class="n">data</span><span class="o">.</span><span class="n">amount</span><span class="p">,</span> <span class="n">l</span><span class="p">[</span><span class="mi">2</span><span class="p">])</span>
                        <span class="n">X</span><span class="p">[</span><span class="n">data</span><span class="o">.</span><span class="n">code</span><span class="p">]</span><span class="o">.</span><span class="n">put_nowait</span><span class="p">(</span><span class="n">temp</span><span class="p">)</span>
                        <span class="n">pair_table</span><span class="o">.</span><span class="n">append</span><span class="p">(</span>
                            <span class="p">[</span><span class="n">data</span><span class="o">.</span><span class="n">code</span><span class="p">,</span> <span class="n">data</span><span class="o">.</span><span class="n">datetime</span><span class="p">,</span> <span class="n">l</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="nb">abs</span><span class="p">(</span><span class="n">data</span><span class="o">.</span><span class="n">amount</span><span class="p">),</span> <span class="n">data</span><span class="o">.</span><span class="n">price</span><span class="p">,</span> <span class="n">l</span><span class="p">[</span><span class="mi">2</span><span class="p">]])</span>
                        <span class="k">break</span>
                    <span class="k">elif</span> <span class="n">l</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">&lt;</span> <span class="p">(</span><span class="nb">abs</span><span class="p">(</span><span class="n">data</span><span class="o">.</span><span class="n">amount</span><span class="p">)):</span>
                        <span class="n">data</span><span class="o">.</span><span class="n">amount</span> <span class="o">=</span> <span class="n">data</span><span class="o">.</span><span class="n">amount</span><span class="o">+</span><span class="n">l</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span>
                        <span class="n">pair_table</span><span class="o">.</span><span class="n">append</span><span class="p">(</span>
                            <span class="p">[</span><span class="n">data</span><span class="o">.</span><span class="n">code</span><span class="p">,</span> <span class="n">data</span><span class="o">.</span><span class="n">datetime</span><span class="p">,</span> <span class="n">l</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="n">l</span><span class="p">[</span><span class="mi">1</span><span class="p">],</span> <span class="n">data</span><span class="o">.</span><span class="n">price</span><span class="p">,</span> <span class="n">l</span><span class="p">[</span><span class="mi">2</span><span class="p">]])</span>
        <span class="n">pair_title</span> <span class="o">=</span> <span class="p">[</span><span class="s1">&#39;code&#39;</span><span class="p">,</span> <span class="s1">&#39;sell_date&#39;</span><span class="p">,</span> <span class="s1">&#39;buy_date&#39;</span><span class="p">,</span>
                      <span class="s1">&#39;amount&#39;</span><span class="p">,</span> <span class="s1">&#39;sell_price&#39;</span><span class="p">,</span> <span class="s1">&#39;buy_price&#39;</span><span class="p">]</span>
        <span class="n">pnl</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">(</span><span class="n">pair_table</span><span class="p">,</span> <span class="n">columns</span><span class="o">=</span><span class="n">pair_title</span><span class="p">)</span><span class="o">.</span><span class="n">set_index</span><span class="p">(</span><span class="s1">&#39;code&#39;</span><span class="p">)</span>
        <span class="n">pnl</span> <span class="o">=</span> <span class="n">pnl</span><span class="o">.</span><span class="n">assign</span><span class="p">(</span><span class="n">pnl_ratio</span><span class="o">=</span><span class="p">(</span><span class="n">pnl</span><span class="o">.</span><span class="n">sell_price</span><span class="o">/</span><span class="n">pnl</span><span class="o">.</span><span class="n">buy_price</span><span class="p">)</span> <span class="o">-</span>
                         <span class="mi">1</span><span class="p">)</span>
        <span class="n">pnl</span> <span class="o">=</span> <span class="n">pnl</span><span class="o">.</span><span class="n">assign</span><span class="p">(</span><span class="n">pnl_money</span><span class="o">=</span><span class="n">pnl</span><span class="o">.</span><span class="n">pnl_ratio</span><span class="o">*</span><span class="n">pnl</span><span class="o">.</span><span class="n">amount</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">pnl</span>

    <span class="nd">@property</span>
    <span class="nd">@lru_cache</span><span class="p">()</span>
    <span class="k">def</span> <span class="nf">pnl_fifo</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="n">X</span> <span class="o">=</span> <span class="nb">dict</span><span class="p">(</span><span class="nb">zip</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">account</span><span class="o">.</span><span class="n">code</span><span class="p">,</span> <span class="p">[</span><span class="n">deque</span><span class="p">()</span>
                                         <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">account</span><span class="o">.</span><span class="n">code</span><span class="p">))]))</span>
        <span class="n">pair_table</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="k">for</span> <span class="n">_</span><span class="p">,</span> <span class="n">data</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">account</span><span class="o">.</span><span class="n">history_table</span><span class="o">.</span><span class="n">iterrows</span><span class="p">():</span>
            <span class="k">if</span> <span class="n">data</span><span class="o">.</span><span class="n">amount</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">:</span>
                <span class="n">X</span><span class="p">[</span><span class="n">data</span><span class="o">.</span><span class="n">code</span><span class="p">]</span><span class="o">.</span><span class="n">append</span><span class="p">((</span><span class="n">data</span><span class="o">.</span><span class="n">datetime</span><span class="p">,</span> <span class="n">data</span><span class="o">.</span><span class="n">amount</span><span class="p">,</span> <span class="n">data</span><span class="o">.</span><span class="n">price</span><span class="p">))</span>
            <span class="k">elif</span> <span class="n">data</span><span class="o">.</span><span class="n">amount</span> <span class="o">&lt;</span> <span class="mi">0</span><span class="p">:</span>
                <span class="k">while</span> <span class="kc">True</span><span class="p">:</span>
                    <span class="n">l</span> <span class="o">=</span> <span class="n">X</span><span class="p">[</span><span class="n">data</span><span class="o">.</span><span class="n">code</span><span class="p">]</span><span class="o">.</span><span class="n">popleft</span><span class="p">()</span>
                    <span class="k">if</span> <span class="n">l</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">==</span> <span class="nb">abs</span><span class="p">(</span><span class="n">data</span><span class="o">.</span><span class="n">amount</span><span class="p">):</span>
                        <span class="n">pair_table</span><span class="o">.</span><span class="n">append</span><span class="p">(</span>
                            <span class="p">[</span><span class="n">data</span><span class="o">.</span><span class="n">code</span><span class="p">,</span> <span class="n">data</span><span class="o">.</span><span class="n">datetime</span><span class="p">,</span> <span class="n">l</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="nb">abs</span><span class="p">(</span><span class="n">data</span><span class="o">.</span><span class="n">amount</span><span class="p">),</span> <span class="n">data</span><span class="o">.</span><span class="n">price</span><span class="p">,</span> <span class="n">l</span><span class="p">[</span><span class="mi">2</span><span class="p">]])</span>
                        <span class="k">break</span>
                    <span class="k">if</span> <span class="n">l</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">&gt;</span> <span class="nb">abs</span><span class="p">(</span><span class="n">data</span><span class="o">.</span><span class="n">amount</span><span class="p">):</span>
                        <span class="n">temp</span> <span class="o">=</span> <span class="p">(</span><span class="n">l</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="n">l</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span><span class="o">+</span><span class="n">data</span><span class="o">.</span><span class="n">amount</span><span class="p">,</span> <span class="n">l</span><span class="p">[</span><span class="mi">2</span><span class="p">])</span>
                        <span class="n">X</span><span class="p">[</span><span class="n">data</span><span class="o">.</span><span class="n">code</span><span class="p">]</span><span class="o">.</span><span class="n">appendleft</span><span class="p">(</span><span class="n">temp</span><span class="p">)</span>
                        <span class="n">pair_table</span><span class="o">.</span><span class="n">append</span><span class="p">(</span>
                            <span class="p">[</span><span class="n">data</span><span class="o">.</span><span class="n">code</span><span class="p">,</span> <span class="n">data</span><span class="o">.</span><span class="n">datetime</span><span class="p">,</span> <span class="n">l</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="nb">abs</span><span class="p">(</span><span class="n">data</span><span class="o">.</span><span class="n">amount</span><span class="p">),</span> <span class="n">data</span><span class="o">.</span><span class="n">price</span><span class="p">,</span> <span class="n">l</span><span class="p">[</span><span class="mi">2</span><span class="p">]])</span>
                        <span class="k">break</span>
                    <span class="k">elif</span> <span class="n">l</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">&lt;</span> <span class="p">(</span><span class="nb">abs</span><span class="p">(</span><span class="n">data</span><span class="o">.</span><span class="n">amount</span><span class="p">)):</span>
                        <span class="n">data</span><span class="o">.</span><span class="n">amount</span> <span class="o">=</span> <span class="n">data</span><span class="o">.</span><span class="n">amount</span><span class="o">+</span><span class="n">l</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span>
                        <span class="n">pair_table</span><span class="o">.</span><span class="n">append</span><span class="p">(</span>
                            <span class="p">[</span><span class="n">data</span><span class="o">.</span><span class="n">code</span><span class="p">,</span> <span class="n">data</span><span class="o">.</span><span class="n">datetime</span><span class="p">,</span> <span class="n">l</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="n">l</span><span class="p">[</span><span class="mi">1</span><span class="p">],</span> <span class="n">data</span><span class="o">.</span><span class="n">price</span><span class="p">,</span> <span class="n">l</span><span class="p">[</span><span class="mi">2</span><span class="p">]])</span>

        <span class="n">pair_title</span> <span class="o">=</span> <span class="p">[</span><span class="s1">&#39;code&#39;</span><span class="p">,</span> <span class="s1">&#39;sell_date&#39;</span><span class="p">,</span> <span class="s1">&#39;buy_date&#39;</span><span class="p">,</span>
                      <span class="s1">&#39;amount&#39;</span><span class="p">,</span> <span class="s1">&#39;sell_price&#39;</span><span class="p">,</span> <span class="s1">&#39;buy_price&#39;</span><span class="p">]</span>
        <span class="n">pnl</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">(</span><span class="n">pair_table</span><span class="p">,</span> <span class="n">columns</span><span class="o">=</span><span class="n">pair_title</span><span class="p">)</span><span class="o">.</span><span class="n">set_index</span><span class="p">(</span><span class="s1">&#39;code&#39;</span><span class="p">)</span>
        <span class="n">pnl</span> <span class="o">=</span> <span class="n">pnl</span><span class="o">.</span><span class="n">assign</span><span class="p">(</span><span class="n">pnl_ratio</span><span class="o">=</span><span class="p">(</span><span class="n">pnl</span><span class="o">.</span><span class="n">sell_price</span><span class="o">/</span><span class="n">pnl</span><span class="o">.</span><span class="n">buy_price</span><span class="p">)</span> <span class="o">-</span>
                         <span class="mi">1</span><span class="p">)</span>
        <span class="n">pnl</span> <span class="o">=</span> <span class="n">pnl</span><span class="o">.</span><span class="n">assign</span><span class="p">(</span><span class="n">pnl_money</span><span class="o">=</span><span class="n">pnl</span><span class="o">.</span><span class="n">pnl_ratio</span><span class="o">*</span><span class="n">pnl</span><span class="o">.</span><span class="n">amount</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">pnl</span>

<div class="viewcode-block" id="QA_Performance.plot_pnlratio"><a class="viewcode-back" href="../../../source/QUANTAXIS.QAARP.html#QUANTAXIS.QAARP.QARisk.QA_Performance.plot_pnlratio">[docs]</a>    <span class="k">def</span> <span class="nf">plot_pnlratio</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">pnl</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        画出pnl比率散点图</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">plt</span><span class="o">.</span><span class="n">scatter</span><span class="p">(</span><span class="n">x</span><span class="o">=</span><span class="n">pnl</span><span class="o">.</span><span class="n">sell_date</span><span class="p">,</span> <span class="n">y</span><span class="o">=</span><span class="n">pnl</span><span class="o">.</span><span class="n">pnl_ratio</span><span class="p">)</span>
        <span class="n">plt</span><span class="o">.</span><span class="n">show</span><span class="p">()</span></div>

<div class="viewcode-block" id="QA_Performance.plot_pnlmoney"><a class="viewcode-back" href="../../../source/QUANTAXIS.QAARP.html#QUANTAXIS.QAARP.QARisk.QA_Performance.plot_pnlmoney">[docs]</a>    <span class="k">def</span> <span class="nf">plot_pnlmoney</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">pnl</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        画出pnl盈亏额散点图</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">plt</span><span class="o">.</span><span class="n">scatter</span><span class="p">(</span><span class="n">x</span><span class="o">=</span><span class="n">pnl</span><span class="o">.</span><span class="n">sell_date</span><span class="p">,</span> <span class="n">y</span><span class="o">=</span><span class="n">pnl</span><span class="o">.</span><span class="n">pnl_money</span><span class="p">)</span>
        <span class="n">plt</span><span class="o">.</span><span class="n">show</span><span class="p">()</span></div>

<div class="viewcode-block" id="QA_Performance.abnormal_active"><a class="viewcode-back" href="../../../source/QUANTAXIS.QAARP.html#QUANTAXIS.QAARP.QARisk.QA_Performance.abnormal_active">[docs]</a>    <span class="k">def</span> <span class="nf">abnormal_active</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        账户的成交发生异常成交记录的分析</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">pass</span></div>

<div class="viewcode-block" id="QA_Performance.brinson"><a class="viewcode-back" href="../../../source/QUANTAXIS.QAARP.html#QUANTAXIS.QAARP.QARisk.QA_Performance.brinson">[docs]</a>    <span class="k">def</span> <span class="nf">brinson</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Brinson Model analysis</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">pass</span></div>

<div class="viewcode-block" id="QA_Performance.hold"><a class="viewcode-back" href="../../../source/QUANTAXIS.QAARP.html#QUANTAXIS.QAARP.QARisk.QA_Performance.hold">[docs]</a>    <span class="k">def</span> <span class="nf">hold</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;持仓分析</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">pass</span></div>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">accumulate_return</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        returns a pd-Dataframe format accumulate return for different periods</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">pass</span>

<div class="viewcode-block" id="QA_Performance.save"><a class="viewcode-back" href="../../../source/QUANTAXIS.QAARP.html#QUANTAXIS.QAARP.QARisk.QA_Performance.save">[docs]</a>    <span class="k">def</span> <span class="nf">save</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;save the performance analysis result to database</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">pass</span></div></div>
</pre></div>

           </div>
           
          </div>
          <footer>
  

  <hr/>

  <div role="contentinfo">
    <p>
        &copy; Copyright 2018, yutiansut.

    </p>
  </div>
  Built with <a href="http://sphinx-doc.org/">Sphinx</a> using a <a href="https://github.com/rtfd/sphinx_rtd_theme">theme</a> provided by <a href="https://readthedocs.org">Read the Docs</a>. 

</footer>

        </div>
      </div>

    </section>

  </div>
  


  

    <script type="text/javascript">
        var DOCUMENTATION_OPTIONS = {
            URL_ROOT:'../../../',
            VERSION:'1.0.39',
            LANGUAGE:'python',
            COLLAPSE_INDEX:false,
            FILE_SUFFIX:'.html',
            HAS_SOURCE:  true,
            SOURCELINK_SUFFIX: '.txt'
        };
    </script>
      <script type="text/javascript" src="../../../_static/jquery.js"></script>
      <script type="text/javascript" src="../../../_static/underscore.js"></script>
      <script type="text/javascript" src="../../../_static/doctools.js"></script>
      <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.1/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>

  

  
  
    <script type="text/javascript" src="../../../_static/js/theme.js"></script>
  

  <script type="text/javascript">
      jQuery(function () {
          SphinxRtdTheme.Navigation.enable(true);
      });
  </script> 

</body>
</html>